﻿console.log('这是通用脚本注入器');

if (location.href.startsWith('https://xxljob-admin-daily.hszq8.com/jobinfo')) {
    console.log('✅ 检测到进入 xxl uat job页面 任务管理页面');
    injectCustomJs('js/xxl-job.js');
}

if (location.href.startsWith('https://xxljob-admin.hszq8.com/jobinfo')) {
    console.log('✅ 检测到进入 xxl prod job页面 任务管理页面');
    injectCustomJs('js/xxl-job.js');
}
if (location.href.startsWith('https://xxljob-admin-feature.hszq8.com/jobinfo')) {
    console.log('✅ 检测到进入 xxl prod job页面 任务管理页面');
    injectCustomJs('js/xxl-job.js');
}
if (location.href.startsWith('https://cms-daily.hszq.com.cn/old/config/genera')
    || location.href.startsWith('https://admin-daily.hszq.com.cn/config/general')
    || location.href.startsWith('https://cms-daily.vbkrhk.com/old/config/genera')
    || location.href.startsWith('https://admin-daily.vbkrhk.com/config/general')) {
    console.log('✅ 检测到进入 平台 daily  通用配置页面');
    injectCustomJs('js/app_config.js');
}
if (location.href.startsWith('https://cms-feature.hszq.com.cn/old/config/genera')
    || location.href.startsWith('https://admin-feature.hszq.com.cn/config/genera')
    || location.href.startsWith('https://cms-feature.vbkrhk.com/old/config/genera')
    || location.href.startsWith('https://admin-feature.vbkrhk.com/config/genera')) {
    console.log('✅ 检测到进入 平台 feature  通用配置页面');
    injectCustomJs('js/app_config.js');
}
if (location.href.startsWith('https://admin.hszq.com.cn/old/config/genera')
    || location.href.startsWith('https://admin.hszq.com.cn/config/genera')
    || location.href.startsWith('https://cms.vbkrhk.com/old/config/genera')
    || location.href.startsWith('https://admin.vbkrhk.com/config/genera')) {
    console.log('✅ 检测到进入 平台 prod  通用配置页面');
    injectCustomJs('js/app_config.js');
}
function domReady(fn) {
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', fn);
    } else {
        fn();
    }
}

domReady(() => {
    console.log('DOM Ready');
    if (/^https:\/\/admin(?:-[a-z]+)?\.vbkrhk\.com\/crmproccess\/task\/pro?ccess\//.test(location.href)) {
        console.log('✅ 检测到进入 crmproccess 任务详情页面');
        injectCustomJs('js/crmproccess-detail.js');
    }
});

function initCustomPanel() {
    var panel = document.createElement('div');
    panel.className = 'chrome-plugin-demo-panel';
    panel.innerHTML = `
		<h2>injected-script操作content-script演示区：</h2>
		<div class="btn-area">
			<a href="javascript:sendMessageToContentScriptByPostMessage('你好，我是普通页面！')">通过postMessage发送消息给content-script</a><br>
			<a href="javascript:sendMessageToContentScriptByEvent('你好啊！我是通过DOM事件发送的消息！')">通过DOM事件发送消息给content-script</a><br>
			<a href="javascript:invokeContentScript('sendMessageToBackground()')">发送消息到后台或者popup</a><br>
		</div>
		<div id="my_custom_log">
		</div>
	`;
    document.body.appendChild(panel);
}

// 向页面注入JS
function injectCustomJs(jsPath) {
    jsPath = jsPath || 'js/inject.js';
    var temp = document.createElement('script');
    temp.setAttribute('type', 'text/javascript');
    // 获得的地址类似：chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject.js
    temp.src = chrome.extension.getURL(jsPath);
    temp.onload = function () {
        // 放在页面不好看，执行完后移除掉
        this.parentNode.removeChild(this);
    };
    document.body.appendChild(temp);
}

// 接收来自后台的消息
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    console.log('收到来自 ' + (sender.tab ? "content-script(" + sender.tab.url + ")" : "popup或者background") + ' 的消息：', request);
    if (request.cmd == 'update_font_size') {
        var ele = document.createElement('style');
        ele.innerHTML = `* {font-size: ${request.size}px !important;}`;
        document.head.appendChild(ele);
    } else {
        tip(JSON.stringify(request));
        sendResponse('我收到你的消息了：' + JSON.stringify(request));
    }
});

// 主动发送消息给后台
// 要演示此功能，请打开控制台主动执行sendMessageToBackground()
function sendMessageToBackground(message) {
    chrome.runtime.sendMessage({greeting: message || '你好，我是content-script呀，我主动发消息给后台！'}, function (response) {
        tip('收到来自后台的回复：' + response);
    });
}

// 监听长连接
chrome.runtime.onConnect.addListener(function (port) {
    console.log(port);
    if (port.name == 'test-connect') {
        port.onMessage.addListener(function (msg) {
            console.log('收到长连接消息：', msg);
            tip('收到长连接消息：' + JSON.stringify(msg));
            if (msg.question == '你是谁啊？') port.postMessage({answer: '我是你爸！'});
        });
    }
});

window.addEventListener("message", function (e) {
    console.log('收到消息：', e.data);
    if (e.data && e.data.cmd == 'invoke') {
        eval('(' + e.data.code + ')');
    } else if (e.data && e.data.cmd == 'message') {
        tip(e.data.data);
    }
}, false);


function initCustomEventListen() {
    var hiddenDiv = document.getElementById('myCustomEventDiv');
    if (!hiddenDiv) {
        hiddenDiv = document.createElement('div');
        hiddenDiv.style.display = 'none';
        hiddenDiv.id = 'myCustomEventDiv';
        document.body.appendChild(hiddenDiv);
    }
    hiddenDiv.addEventListener('myCustomEvent', function () {
        var eventData = document.getElementById('myCustomEventDiv').innerText;
        tip('收到自定义事件：' + eventData);
    });
}

var tipCount = 0;

// 简单的消息通知
function tip(info) {
    info = info || '';
    var ele = document.createElement('div');
    ele.className = 'chrome-plugin-simple-tip slideInLeft';
    ele.style.top = tipCount * 70 + 20 + 'px';
    ele.innerHTML = `<div>${info}</div>`;
    document.body.appendChild(ele);
    ele.classList.add('animated');
    tipCount++;
    setTimeout(() => {
        ele.style.top = '-100px';
        setTimeout(() => {
            ele.remove();
            tipCount--;
        }, 400);
    }, 3000);
}
